﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLog;
using DAL;
using System.Data;

public partial class MobShopSport : System.Web.UI.Page
{

    #region Variables
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    private const int PageSize = 25;
    private DataSet objDataSet;
    #endregion

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            PageTitle.Text = "تابع اخر و احدث اخبار سوق التنقلات للاعبيين |  LiveKora  لايف كورة";
            Page.MetaDescription = "تابع اخر و احدث اخبار سوق التنقلات للاعبيين ";
            plcPaging.Controls.Clear();
            ManageArticls objManageArticls = new ManageArticls();
            objDataSet = objManageArticls.GetAllCatigoryArticlsFull(19, 1 - 1, PageSize).Tables[0].DataSet;
            CreatePagingControl(objDataSet.Tables[1].Rows.Count);
            if (!Page.IsPostBack)
            {
                ViewState["PAGE"] = 1;
                FetchData(1);
            }
        }
        catch (Exception ex)
        {
            _logger.Error("MobShopSport:::Mobile Version::::Page_Load:::" + ex.Message);
        }
    }
    #endregion

    #region Events
    void lbl_Click(object sender, EventArgs e)
    {
        try
        {
            int lastPage = Convert.ToInt32(ViewState["PAGE"]);
            LinkButton lastLink = (LinkButton)plcPaging.FindControl("pLink" + lastPage.ToString());
            lastLink.Style.Add("background", "#333");
            LinkButton lnk = sender as LinkButton;
            int currentPage = int.Parse(lnk.Text);
            lnk.Style.Add("background", "#789");
            FetchData(currentPage);
            ViewState["PAGE"] = currentPage;
        }
        catch (Exception ex)
        {
            _logger.Error("CategoryArticles:::lbl_Click:::" + ex.Message);

        }
    }
    #endregion


    #region Methods
    private void CreatePagingControl(int rowCount)
    {
        try
        {
            rowCount = (rowCount / PageSize) + 1;
            for (int i = rowCount - 1; i >= 0; i--)
            {
                LinkButton lnk = new LinkButton();
                lnk.Click += new EventHandler(lbl_Click);
                lnk.ID = "lnkPage" + (i + 1).ToString();
                lnk.Text = (i + 1).ToString();
                if (i == 0)
                    lnk.Style.Value = "background: #789;padding: 3px;padding-left: 8px;padding-right: 8px;text-decoration: none;color: #FFF;margin: 2px;";
                else
                    lnk.Style.Value = "background: #333;padding: 3px;padding-left: 8px;padding-right: 8px;text-decoration: none;color: #FFF;margin: 2px;";
                lnk.ID = "pLink" + (i + 1).ToString();
                plcPaging.Controls.Add(lnk);
                Label spacer = new Label();
                spacer.Text = "&nbsp;";
                plcPaging.Controls.Add(spacer);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void FetchData(int pageIndex)
    {
        try
        {
            ManageArticls objManageArticls = new ManageArticls();
            rptCategoryArticles.DataSource = objManageArticls.GetAllCatigoryArticlsFull(19, pageIndex - 1, PageSize);
            rptCategoryArticles.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected string GetDateFormat(string sDataTime)
    {
        try
        {
            return Convert.ToDateTime(sDataTime).ToShortDateString();
        }
        catch (Exception)
        {
            return "Unknown";
        }
    }
    /// <summary>
    /// Generate URL
    /// </summary>
    /// <param name="Title"></param>
    /// <param name="strId"></param>
    /// <returns></returns>
    protected string GenerateURL(object Title, object strId)
    {
        try
        {
            string strTitle = Title.ToString();

            //#region Generate SEO Friendly URL based on Title

            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');

            strTitle = strTitle.ToLower();
            char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
            strTitle = strTitle.Replace("c#", "C-Sharp");
            strTitle = strTitle.Replace("vb.net", "VB-Net");
            strTitle = strTitle.Replace("asp.net", "Asp-Net");
            strTitle = strTitle.Replace(".", "-");
            for (int i = 0; i < chars.Length; i++)
            {
                string strChar = chars.GetValue(i).ToString();
                if (strTitle.Contains(strChar))
                {
                    strTitle = strTitle.Replace(strChar, string.Empty);
                }
            }
            strTitle = strTitle.Replace(" ", "-");

            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("-----", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');
            strTitle = "../../../mobile/Article/" + strId + "/";
            return strTitle;
        }
        catch (Exception)
        {
            return "";
        }
    }
    #endregion
}